package cn.com.nxt.txb.fragment;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import cn.com.nxt.txb.AboutActivity;
import cn.com.nxt.txb.LoginActivity;
import cn.com.nxt.txb.MainActivity;
import cn.com.nxt.txb.ModifyPasswordActivity;
import cn.com.nxt.txb.ModifyUsernameActivity;
import cn.com.nxt.txb.MyMeetingActivity;
import cn.com.nxt.txb.R;
import cn.com.nxt.txb.RegisterActivity;
import cn.com.nxt.txb.RetrievePasswordActivity;
import cn.com.nxt.txb.UserInfoActivity;
import cn.com.nxt.txb.model.User;
import cn.com.nxt.txb.util.AppUtil;
import cn.com.nxt.txb.util.LogoutHelper;
import cn.com.nxt.txb.widget.ClearEditText;

@ContentView(R.layout.fragment_mine)//加载布局
public class MineFragment extends BaseFragment {
    public static boolean isUpdateUsername = false;
    public static boolean isLogout = false;
    public static final byte RESULT_LOGOUT = 0x002;//退出时返回
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    @ViewInject(R.id.tv_username)
    private TextView mUsernameTv;
    @ViewInject(R.id.tv_phone)
    private TextView mPhoneTv;

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment MineFragment.
     */
    public static MineFragment newInstance(String param1, String param2) {
        MineFragment fragment = new MineFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {

        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    @Override
    public void onResume() {
        super.onResume();
        if (isUpdateUsername) {
            mUsernameTv.setText(AppUtil.mUser.getUsername());
            isUpdateUsername = false;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = x.view().inject(this, inflater, container);
        //showToast("用户名：" + AppUtil.mUser.getUsername());
        if (AppUtil.mUser != null) {
            mUsernameTv.setText(AppUtil.mUser.getUsername());
            mPhoneTv.setText("账号：" + AppUtil.mUser.getPhone());
        }
        return view;//fragment注解
    }

    @Event({R.id.ll_user_info, R.id.ll_about,R.id.ll_set})
    private void onViewClick(View v) {
        switch (v.getId()) {
            case R.id.ll_user_info:
                skipUserInfo();
                break;
            case R.id.ll_about:
                startActivity(new Intent(getActivity(), AboutActivity.class));
                break;
            case R.id.ll_set:
                startActivity(new Intent(getActivity(), MyMeetingActivity.class));
                break;
        }
    }

    private void skipUserInfo() {
        Intent intent = new Intent(getActivity(), UserInfoActivity.class);
        startActivityForResult(intent, RESULT_LOGOUT);
    }


    @Override
    public void onActivityResult(int arg0, int arg1, Intent data) {
        super.onActivityResult(arg0, arg1, data);
        if (arg0 == RESULT_LOGOUT) {
            if (!isLogout) {
                isLogout = false;
                return;
            }
            //showToast("123456");
            AppUtil.clearUser(getActivity());
            AppUtil.mUser = null;
            LogoutHelper.logout(getActivity(), false);
        }
    }
}
